

import UIKit
import MBProgressHUD
import SnapKit

enum HUD {
    
    static func showMessage(_ message: String?, offset: CGFloat = screenHeight * 0.5 - 200) {
        showMessage(message, inView: nil, offset: offset)
    }
    
    static func showErrorMessage(_ error: Error?, offset: CGFloat = screenHeight * 0.5 - 200) {
        guard let dataError = error as? SCAError else { return }
        
        if  case .universal(let message) = dataError {
            showMessage(message, inView: nil, offset: offset)
        } else {
            showMessage(dataError.localizedDescription, inView: nil, offset: offset)
        }
    }

    static func showMessage(_ message: String?, inView: UIView?, offset: CGFloat = screenHeight * 0.5 - 200) {
        
        AppDataStore.share.currentHud?.hide(animated: true)
     
        let keyWindow = UIApplication.shared.delegate?.window!
        let targetView = inView ?? keyWindow
        
        if targetView != nil {
            MBProgressHUD.hide(for: targetView!, animated: false)
        }

        let hud = MBProgressHUD.showAdded(to: targetView!, animated: true)
        hud.isUserInteractionEnabled = false  
        hud.mode = .text
        hud.label.text = message
        hud.label.numberOfLines = 10
        hud.label.font = UIFontMake(15);
        hud.label.textColor = .white;
        hud.margin = 10;
        hud.bezelView.style = .solidColor;
        hud.removeFromSuperViewOnHide = true
        hud.bezelView.backgroundColor = UIColorMake(0x333333).withAlphaComponent(0.9)
        hud.contentColor = .white
        hud.offset = CGPoint(x: 0, y: offset)
        AppDataStore.share.currentHud = hud
        hud.hide(animated: true, afterDelay: 2.0)
    }
    
    static func showLoadingInView(_ view: UIView? = nil, text: String? = nil) {
        
        AppDataStore.share.currentHud?.hide(animated: true)

        let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
        guard let targetView = view ?? keyWindow else {
            return
        }
        
        MBProgressHUD.hide(for: targetView, animated: false)

        let hud = MBProgressHUD.showAdded(to: targetView, animated: true)
        hud.mode = .indeterminate
        hud.bezelView.style = .solidColor;
        hud.bezelView.backgroundColor = UIColorMake(0x333333).withAlphaComponent(0.9)
        hud.label.text = text
        hud.contentColor = .white
        hud.offset.y = 0
        hud.label.numberOfLines = 0
        AppDataStore.share.currentHud = hud
    }
    
    static func hideHUD(for view: UIView? = nil) {
        let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
        guard let targetView = view ?? keyWindow else {
            return
        }
        
        MBProgressHUD.hide(for: targetView, animated: true)
        AppDataStore.share.currentHud = nil
    }
    
    static func showActivityIndicator(in view: UIView? = nil) {
        
        let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
        guard let targetView = view ?? keyWindow else {
            return
        }
        
        let activityIndicator = UIActivityIndicatorView(style: .medium)
        activityIndicator.hidesWhenStopped = true
        activityIndicator.color = .gray
        activityIndicator.startAnimating()
        activityIndicator.transform = CGAffineTransformMakeScale(1.2, 1.2)
        targetView.addSubview(activityIndicator)
        activityIndicator.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        AppDataStore.share.activityIndicator = activityIndicator
    }
    
    static func hideActivityIndicator() {
        guard let activityIndicator = AppDataStore.share.activityIndicator else {
            return
        }
        
        activityIndicator.removeFromSuperview()
        activityIndicator.stopAnimating()
        AppDataStore.share.activityIndicator = nil
    }

}
